package com.sfa.services;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日志模块服务类
 * =============================================================
 * Version          Author          Date            Description
 * 1.0              白建         2017/11/1             创建
 * =============================================================
 */
public class LogService
{

    private static final Logger LOG = LoggerFactory.getLogger(LogService.class);

    public String readFileByLines(String logPath)
    {

        File file = new File(logPath);
        BufferedReader reader = null;
        FileReader fr = null;
        Queue<String> queue = new LinkedList<String>();
        try
        {
            fr = new FileReader(file);
            reader = new BufferedReader(fr);
            String tempString;
            int line = 0;
            while ((tempString = reader.readLine()) != null)
            {
                line++;
                if (line > 500)
                {
                    queue.poll();
                }
                queue.offer(tempString);
            }
            fr.close();
            reader.close();
        }
        catch (IOException e)
        {
            LOG.error(e.getMessage());
        }
        finally
        {
            if (fr != null)
            {
                try
                {
                    fr.close();
                }
                catch (IOException e1)
                {
                    LOG.error(e1.getMessage());
                }
            }
            if (reader != null)
            {
                try
                {
                    reader.close();
                }
                catch (IOException e2)
                {
                    LOG.error(e2.getMessage());
                }
            }
        }
        return queue.toString();
    }
}
